<template>
    <div>
        <el-menu
          default-active="1-4-1"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          :collapse="isCollapse"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b">
            <h3>{{ isCollapse ? '通用' : '通用后台管理系统' }}</h3>
            <el-menu-item v-for="item in noChildren" :key="item.name" :index="item.name" @click="goPage(item)">
                <i :class="`el-icon-${item.icon}`"></i>
                <span slot="title">{{ item.lable }}</span>
            </el-menu-item>
            <el-submenu v-for="item in hasChildren" :key="item.name" :index="item.name">
                <template slot="title">
                <i class="el-icon-location"></i>
                <span slot="title">{{ item.lable }}</span>
                </template>
                <el-menu-item-group v-for="chilItem in item.children" :key="chilItem.name">
                    <el-menu-item :index="chilItem.name" @click="goPage(chilItem)">{{ chilItem.lable }}</el-menu-item>
                </el-menu-item-group>
            </el-submenu>
        </el-menu>
    </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
    name: 'mainAside',
    data() {
        return {
            menuData: [
                {
                    path: "/home",
                    name: "home",
                    lable: "首页",
                    icon: "s-home"
                },
                {
                    path: "/user",
                    name: "user",
                    lable: "用户管理",
                    icon: "user"
                },
                {
                    path: "/system",
                    name: "system",
                    lable: "系统管理",
                    icon: "menu"
                },
                {
                    name: "other",
                    lable: "其它",
                    icon: "data",
                    children: [
                        {
                            path: "/other",
                            name: "other1",
                            lable: "页面1",
                            url: "Home/Home"
                        },
                        {
                            path: "/other2",
                            name: "other2",
                            lable: "页面2",
                            url: "Home/Home"
                        },
                    ]
                },
            ]
        }
    },
    computed: {
        noChildren() {
            return this.menuData.filter(item => !item.children)
        },
        hasChildren() {
            return this.menuData.filter(item => item.children)
        },
        // isCollapse() {
        //     return this.$store.state.tab.isCollapse
        // }
        ...mapState('tab',['isCollapse'])

    },
    created() {
        console.log(this.isCollapse)
    },
    methods: {
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        goPage(item) {
            if(this.$route.path !== item.path ) {
                this.$store.commit('tab/changeTabs',item)
                this.$router.push(item.path)
            }
        }
    }
}
</script>
<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
.el-menu{
    height: 100vh;
    border-right: none;
    h3{
        color: #FFF;
        text-align: center;
        line-height: 48px;
        font-size: 16px;
        font-weight: 400;
    }
}
</style>